package com.hanhanxiaochong.zhongxiangchuanmei.view.home.act;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.hanhanxiaochong.zhongxiangchuanmei.R;
import com.hanhanxiaochong.zhongxiangchuanmei.bean.HotProjectBean;
import com.hanhanxiaochong.zhongxiangchuanmei.bean.NearByShopBean;
import com.hanhanxiaochong.zhongxiangchuanmei.bean.ShopListBean;
import com.hanhanxiaochong.zhongxiangchuanmei.presenter.SearchPresenter;
import com.hanhanxiaochong.zhongxiangchuanmei.view.base.BaseActivity;
import com.hanhanxiaochong.zhongxiangchuanmei.view.home.adapter.HotProjectAdapter;
import com.hanhanxiaochong.zhongxiangchuanmei.view.home.adapter.NearbyShopAdapter;
import com.hanhanxiaochong.zhongxiangchuanmei.view.iview.ISearchView;
import com.hanhanxiaochong.zhongxiangchuanmei.view.main.adapter.GoodListAdapter;
import com.hanhanxiaochong.zhongxiangchuanmei.view.shop.act.GoodDetailActivity;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 首页搜索
 */
public class SearchActivity extends BaseActivity<SearchPresenter> implements ISearchView, AMapLocationListener {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.et_search)
    EditText etSearch;
    @BindView(R.id.tv_shop)
    TextView tvShop;
    @BindView(R.id.tv_product)
    TextView tvProduct;
    @BindView(R.id.tv_project)
    TextView tvProject;
    @BindView(R.id.search_rv)
    RecyclerView searchRv;

    //声明AMapLocationClient类对象
    public AMapLocationClient mLocationClient = null;
    //声明AMapLocationClientOption对象
    public AMapLocationClientOption mLocationOption = null;
    private double latitude;
    private double longitude;
    private String keyword;

    public static Intent createIntent(Context context, String keyword) {
        Intent intent = new Intent(context, SearchActivity.class);
        intent.putExtra("keyword", keyword);
        return intent;
    }

    @Override
    protected void initView() {
        setContentView(R.layout.activity_search);
        keyword = getIntent().getStringExtra("keyword");
    }

    @Override
    protected void initPresenter() {
        mPresenter = new SearchPresenter(this);
    }

    @Override
    protected void init() {
        initLocation();
        tvShop.setSelected(true);
        searchRv.setLayoutManager(new LinearLayoutManager(this));
        etSearch.setText(keyword);
        etSearch.setOnEditorActionListener((v, actionId, event) -> {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                keyword = etSearch.getText().toString().trim();
                setTabs("1");
                 /*隐藏软键盘*/
                InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                if (inputMethodManager.isActive()) {
                    inputMethodManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), 0);
                }
            }
            return false;
        });
    }

    @OnClick({R.id.iv_back, R.id.tv_shop, R.id.tv_product, R.id.tv_project})
    public void onViewClicked(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_shop:
                setTabs("1");
                break;
            case R.id.tv_product:
                setTabs("2");
                break;
            case R.id.tv_project:
                setTabs("3");
                break;
        }
    }

    /**
     * 设置指示器
     *
     * @param searchType //搜索类型 门店：1（默认）；商品：2；项目：3;
     */
    private void setTabs(String searchType) {
        tvShop.setSelected(searchType.equals("1"));
        tvProduct.setSelected(searchType.equals("2"));
        tvProject.setSelected(searchType.equals("3"));
        switch (searchType) {
            case "1":
                mPresenter.searchShop(keyword, latitude, longitude);
                break;
            case "2":
                mPresenter.searchProduct(keyword);
                break;
            case "3":
                mPresenter.searchProject(keyword);
                break;
        }
    }

    @Override
    public void setShopList(List<NearByShopBean> data) {
        NearbyShopAdapter nearbyShopAdapter = new NearbyShopAdapter(this, R.layout.item_nearby_shop, data);
        searchRv.setAdapter(nearbyShopAdapter);
        nearbyShopAdapter.setmOnItemClickListener((arg0, arg1) -> startActivity(NearbyShopDetailActivity.createIntent(this, data.get(arg1).getId())));
    }

    @Override
    public void setProductList(List<ShopListBean> data) {
        GoodListAdapter goodListAdapter = new GoodListAdapter(this, R.layout.item_shop, data);
        searchRv.setAdapter(goodListAdapter);
        goodListAdapter.setmOnItemClickListener((adapter, position) ->
                startActivity(GoodDetailActivity.createIntent(this, data.get(position).getId(), null)));
    }

    @Override
    public void setProjectList(List<HotProjectBean> data) {
        HotProjectAdapter hotProjectAdapter = new HotProjectAdapter(mContext, R.layout.item_hot_project, data);
        searchRv.setAdapter(hotProjectAdapter);
        hotProjectAdapter.setmOnItemClickListener((adapter, position) ->
                startActivity(HotProjectDetailActivity.createIntent(this, data.get(position).getId())));
    }

    private void initLocation() {
        //初始化定位
        mLocationClient = new AMapLocationClient(getApplicationContext());
        //设置定位回调监听
        mLocationClient.setLocationListener(this);
        //初始化AMapLocationClientOption对象
        mLocationOption = new AMapLocationClientOption();
        //设置定位模式为AMapLocationMode.Hight_Accuracy，高精度模式。
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        mLocationOption.setOnceLocation(true);
        //给定位客户端对象设置定位参数
        mLocationClient.setLocationOption(mLocationOption);
        //启动定位
        mLocationClient.startLocation();
    }

    @Override
    public void onLocationChanged(AMapLocation aMapLocation) {
        latitude = aMapLocation.getLatitude();
        longitude = aMapLocation.getLongitude();
        setTabs("1");
    }
}
